package com.hfi.restful.validator;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 在这个validator中你可以通过autowired注入任何想要的东西
 * MyConstraintValidator 自动注册为spring中的一个bean
 * @author ChangLiang
 * @date 2019/7/29
 *
 * Object说明validate逻辑可以放在任何对象上
 */
public class MyConstraintValidator implements ConstraintValidator<MyConstraint,Object> {

    private Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Override
    public void initialize(MyConstraint constraintAnnotation) {
        LOGGER.info("my constraint validator init");
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value!=null) {
            LOGGER.info("username: {}",value.toString());
        }
        if (StringUtils.equals(value.toString(), "cl")) {
            return true;
        }
        return false;
    }
}
